import Vue from 'vue';
import App from './App.vue';
import VueRouter from 'vue-router';
import router from './routers';
import VueUi from 'vue-fiu-ui';
import './plugins/HttpClient';
import Storage from './plugins/Storage';
import Key from './store/Config';
import Compile from './plugins/Compile';
import Util from './plugins/Util';

import 'vue-fiu-ui/dist/static/css/vue-fiu-ui.css';
import './sass/app.scss';

Vue.use(VueRouter);
Vue.use(VueUi);
Vue.use(Compile);
Vue.use(Key);
Vue.use(Storage);
Vue.use(Util);

router.beforeEach((to, form, next) => {
    try {
        let path = to.path;
        let userInfo = JSON.parse(Compile.compile.uncompileStr(Storage.storage.get(Key.key.USER_DATA)));
        if (path.indexOf('/admin') !== -1) {
            if (userInfo instanceof Object && Util.util.checkedUserPermission(userInfo, 'admin-web')) {
                return next();
            } else {
                return next('/');
            }
        }
        if (path === '/register' || path === '/login') {
            if (userInfo instanceof Object) {
                return next('/');
            }
        }
        if (path === '/logout') {
            if (!(userInfo instanceof Object)) {
                return next('/');
            }
        }
    } catch (e) {
    
    }
    return next();
});

window.__ = new Vue({
    el: '#app',
    router,
    render: h => h(App),
    data: {
        eventHub: new Vue()
    }
});
